<template>
  <div class="mod-user">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="用户名">
        <el-input v-model="dataForm.userName" maxlength="20" placeholder="用户名" :aria-valuemax="20" style="width: 150px;" clearable></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="dataForm.mobile" maxlength="11" style="width: 140px" :aria-valuemax="11" placeholder="手机号" clearable></el-input>
      </el-form-item>
      <el-form-item label="审核状态">
          <el-select v-model="dataForm.auditState" style="width:150px">
              <el-option v-for="item in auditStateArr"
              :key="item.value"
              :label="item.text"
              :value="item.value">
              </el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="账号状态">
        <el-select v-model="dataForm.accountState" style="width:150px">
          <el-option v-for="item in accoutStateArr"
                     :key="item.value"
                     :label="item.text"
                     :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="会员等级">
        <el-select v-model="dataForm.grade" style="width:150px">
          <el-option v-for="item in gradeArr"
                     :key="item.value"
                     :label="item.text"
                     :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">

      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>

      <el-table-column
        prop="mobile"
        header-align="center"
        align="center"
        label="手机号">
      </el-table-column>

      <el-table-column
        prop="userName"
        header-align="center"
        align="center"
        label="姓名">
      </el-table-column>

      <el-table-column
        prop="userName"
        header-align="center"
        align="center"
        label="用户名">
      </el-table-column>

      <el-table-column
        prop="licensePlateNumber"
        header-align="center"
        align="center"
        label="车牌号">
      </el-table-column>

      <el-table-column
        prop="auditState"
        header-align="center"
        align="center"
        label="审核状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.auditState === 0" size="small" type="danger">未提交</el-tag>
          <el-tag v-if="scope.row.auditState === 1" size="small" >审核中</el-tag>
          <el-tag v-if="scope.row.auditState === 2" size="small">驳回</el-tag>
          <el-tag v-if="scope.row.auditState === 3" size="small">通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="accountState"
        header-align="center"
        align="center"
        label="账号状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.accountState === 0" size="small" type="danger">正常</el-tag>
          <el-tag v-if="scope.row.accountState === 1" size="small" >黑名单</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="grade"
        header-align="center"
        align="center"
        label="等级">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.grade === 1" size="small" type="danger">普通</el-tag>
          <el-tag v-if="scope.row.grade === 2"  size="small">银卡</el-tag>
          <el-tag v-if="scope.row.grade === 3"  size="small">金卡</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="integral"
        header-align="center"
        align="center"
        label="积分">
      </el-table-column>

      <el-table-column
        prop="balanceAmount"
        header-align="center"
        align="center"
        label="余额">
      </el-table-column>

        <el-table-column
          prop="cooperationAmount"
          header-align="center"
          align="center"
          label="互助余额">
      </el-table-column>

        <el-table-column
          prop="todayProfit"
          header-align="center"
          align="center"
          label="今日推广收益">
        </el-table-column>

        <el-table-column
          prop="totalProfit"
          header-align="center"
          align="center"
          label="总收益">
        </el-table-column>

      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        width="180"
        label="创建时间">
      </el-table-column>
      <el-table-column
        prop="updateTime"
        header-align="center"
        align="center"
        width="180"
        label="修改时间">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.accountState === 0" size="small" >
            <el-button v-if="isAuth('user:user:addblacklist')" type="text" size="small" @click="blacklistHandle(scope.row.userId,1)">添加黑名单</el-button>
          </el-tag>
          <el-tag v-if="scope.row.accountState === 1" size="small" >
            <el-button v-if="isAuth('user:user:removeblacklist')" type="text" size="small" @click="blacklistHandle(scope.row.userId,0)">移除黑名单</el-button>
          </el-tag>
          <el-tag v-if="scope.row.auditState === 1" size="small" >
            <el-button v-if="isAuth('user:user:audit')" type="text" size="small" @click="addOrUpdateHandle(scope.row.userId)">审核</el-button>
          </el-tag>
          <el-tag v-if="scope.row.auditState === 2 || scope.row.auditState === 3" size="small" >
            <el-button v-if="isAuth('user:user:detail')" type="text" size="small" @click="addOrUpdateHandle(scope.row.userId)">详情</el-button>
          </el-tag>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  import AddOrUpdate from './user-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          userName: '',
          mobile:'',
          auditState:'',
          accountState:'',
          grade:''
        },
        dataList: [],
        gradeArr:[
          {text:"请选择",value:""},
          {text:"普卡",value:"1"},
          {text:"银卡",value:"2"},
          {text:"金卡",value:"3"}
        ],
        auditStateArr:[
          {text:"请选择",value:''},
          {text:"未提交",value:0},
          {text:"审核中",value:1},
          {text:"驳回",value:2},
          {text:"通过",value:3}
          ],
        accoutStateArr:[
          {text:"请选择",value:''},
          {text:"正常",value:0},
          {text:"黑名单",value:1}
        ],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate
    },
    activated () {
      this.getDataList()
    },
    methods: {
      //黑名单处理
      blacklistHandle(userId,accountState){debugger
        var msg="确定要加入黑名单吗？";
        if(accountState==0){
            msg = "确定要移除黑名单吗？"
        }
        this.$confirm(msg, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/cuser/blackList'),
            method: 'post',
            params: this.$http.adornParams({
              'userId': userId,
              'accountState': accountState
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.getDataList();
            }
          })

        }).catch(() => {})
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/cuser/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'userName': this.dataForm.userName,
            'auditState':this.dataForm.auditState,
            'accountState':this.dataForm.accountState,
            'grade':this.dataForm.grade

          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var userIds = id ? [id] : this.dataListSelections.map(item => {
          return item.userId
        })
        this.$confirm(`确定对[id=${userIds.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/sys/user/delete'),
            method: 'post',
            data: this.$http.adornData(userIds, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      }
    }
  }
</script>
